<template>
  <a-card :bordered="false" class="card-area">
    <div v-if="!set">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="12">
              <a-form-item label="学生来源">
                <a-select :allowClear="true" v-model="queryParam.xsly">
                  <a-select-option v-for="i in TYPE0503" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="院系">
                <a-select :disabled="selectMenu.yx.length === 1" :allowClear="true" showSearch :filterOption="filterOption" v-model="queryParam.ssyxm" @change="yxChange">
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="学年">
                <a-select disabled :allowClear="true" v-model="queryParam.xn">
                  <a-select-option v-for="i in TYPE0125" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="学期">
                <a-select disabled :allowClear="true" v-model="queryParam.xq">
                  <a-select-option v-for="i in TYPE0072" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="年级">
                <a-select :allowClear="true" v-model="queryParam.sznj">
                  <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="学生类别">
                <a-select :allowClear="true" v-model="queryParam.xslbm">
                  <a-select-option v-for="i in TYPE0075" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="12">
                <a-form-item label="所属专业">
                  <a-select :allowClear="true" showSearch :filterOption="filterOption" v-model="queryParam.sszym">
                    <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh}} {{ i.zymc }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="12">
                <a-form-item label="学号">
                  <a-input v-model="queryParam.xh"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="12">
                <a-form-item label="姓名">
                  <a-input v-model="queryParam.xm"/>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="10" :sm="20">
              <span class="table-page-search-submitButtons" >
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button type="primary" @click="audit">批量报到</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {ssyxm: selectMenu.yx.length === 1 ? queryParam.ssyxm : '', xn: queryParam.xn, xq: queryParam.xq, xsly: '01'}">重置</a-button>
                <a @click="advanced = !advanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="key"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        :pageSize="parameter.pageSize"
        :pageNum="parameter.pageNum"
        :scroll="{x:1200}"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <template slot="xh" slot-scope="a,b">
          <a @click="direct(b)">{{a}}</a>
        </template>
        <template slot="operation" slot-scope="a,b">
          <a @click="openEdit(b)">报到</a>
        </template>
      </s-table>
    </div>
    <a-form :form="form" v-else>
      <a-row :gutter="48" class="info">
        <a-col :md="6" style="text-align: center">
          <img src="@/assets/avator.jpg" width="200px" alt="">
        </a-col>
        <a-col  :md="6">
          <div style="padding: 10px 0">学号：{{info.xh}}</div>
          <div style="padding: 10px 0">院系：{{info.xjxx && info.xjxx.ssyxmc}}</div>
          <div style="padding: 10px 0">性别：{{info.xbmc}}</div>
          <div style="padding: 10px 0">导师姓名：{{info.xjxx && info.xjxx.dsxm}}</div>
          <div style="padding: 10px 0">培养方向：{{info.xjxx && info.xjxx.pyfxmc}}</div>
          <div style="padding: 10px 0">学年：{{systemInfo.schoolYear}}</div>
        </a-col>
        <a-col  :md="6">
          <div style="padding: 10px 0">姓名：{{info.xm}}</div>
          <div style="padding: 10px 0">专业：{{info.xjxx && info.xjxx.sszymc}}</div>
          <div style="padding: 10px 0">年级：{{info.xjxx && info.xjxx.sznjmc}}</div>
          <div style="padding: 10px 0">身份证：{{info.zjhm}}</div>
          <div style="padding: 10px 0">学生类别：{{info.xjxx && info.xjxx.xslbmc}}</div>
          <div style="padding: 10px 0">学期：{{info.xq}}</div>
        </a-col>
      </a-row>
      <a-row>
        <a-form-item label="报到备注" :wrapperCol="{span: 24}">
          <a-input type="textarea" v-decorator="['note']" :autosize="{ minRows: 2, maxRows: 10 }"/>
        </a-form-item>
      </a-row>
      <a-row style="text-align: center">
        <a-button style="margin-right: 20px;" @click="set = false">返回</a-button>
        <a-button type="primary" @click="send">确定</a-button>
      </a-row>
    </a-form>
  </a-card>
</template>

<script>
// import moment from 'moment'
import { STable } from '~'
import { mapState } from 'vuex'

const selectArr = ['TYPE0311', 'TYPE0312', 'TYPE0126', 'TYPE0075', 'TYPE0056', 'TYPE0083', 'TYPE0125', 'TYPE0072', 'TYPE0503']
export default {
  name: 'TableList',
  components: {
    STable
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: true,
      // 查询参数
      queryParam: {
        xsly: '01'
      },
      set: false,
      postParams: {},
      // 表头
      columns: [
        {
          title: '学号',
          dataIndex: 'xh',
          // scopedSlots: { customRender: 'xh' }
        },
        {
          title: '姓名',
          dataIndex: 'xm'
        },
        {
          title: '性别',
          dataIndex: 'xbmc'
        },
        {
          title: '年级',
          dataIndex: 'sznj'
        },
        {
          title: '院系',
          dataIndex: 'ssyxmc'
        },
        {
          title: '专业',
          dataIndex: 'sszymc'
        },
        {
          title: '学年',
          dataIndex: 'xn'
        },
        {
          title: '学期',
          dataIndex: 'xqmc'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' },
          width:80,
          fixed:'right'
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        if(this.systemInfo && this.selectMenu.yx.length > 0) {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
          return this.$api.student.registerwbdList(params).then(res => {
            return {
              data: res.data.rows,
              pageNo: parameter.pageNo,
              pageSize: parameter.pageSize,
              totalPage: Math.ceil(res.data.total / parameter.pageSize),
              totalCount: res.data.total
            }
          })
        }
      },
      selectedRowKeys: [],
      selectedRows: [],

      // custom table alert & rowSelection
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: true, // 是否显示alert，已选中
      visible: false,
      confirmLoading: false,
      editId: null,
      info: {},
      selectMenu: {
        yx: [], // 院系
        zy: [] // 所属专业
      }
    }
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0125: state => state.app['TYPE0125'], // 学期
      TYPE0072: state => state.app['TYPE0072'], // 学年
      TYPE0311: state => state.app['TYPE0311'], // 学业奖学金等级
      TYPE0312: state => state.app['TYPE0312'], // 助研奖学金等级
      TYPE0126: state => state.app['TYPE0126'], // 年级
      TYPE0075: state => state.app['TYPE0075'], // 学生类别
      TYPE0056: state => state.app['TYPE0056'], // 录取类别
      TYPE0083: state => state.app['TYPE0083'], // 入学方式
      TYPE0503: state => state.app['TYPE0503'], // 入学方式
    })
  },
  beforeCreate () {
    this.form = this.$form.createForm(this)
  },
  created () {
    this.$store.dispatch('app/sysInfo').then(res => {
      this.$set(this.queryParam, 'xn', this.systemInfo.schoolYear)
      this.$set(this.queryParam, 'xq', this.systemInfo.currentSemester)
      this.initSelectMenu()
    })
    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        if(this.selectMenu.yx && this.selectMenu.yx.length == 1){
          this.$set(this.queryParam, 'ssyxm', this.selectMenu.yx[0].dwh)
          this.yxChange(this.queryParam.ssyxm)
        }
        this.$nextTick(() => {
          this.$refs.table.refresh(true)
        })
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    handleCreate () {
      const form = this.$refs.audit.form
      form.validateFields((err, values) => {
        if (err) {
          return
        }
        this.$api.student.registerCheck(Object.assign({xh: this.xh}, values)).then(res => {
          this.$refs.table.refresh(false)
        })
        form.resetFields()
        this.visible = false
      })
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    direct (data) {
      this.$router.push(`/student/stuStatus/search/stuView?xh=${data.xh}`)
    },
    audit () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要报到的学生')
        return
      }
      this.xhs = this.selectedRows.map(v => {
        return v.xh
      }).toString()
      this.$confirm({
        title: '提示',
        content: `确定对学生：${this.xhs}进行报到吗？`,
        okText: '确认',
        cancelText: '取消',
        onOk:() => {
          this.$api.student.registerCheck(Object.assign({xhs: this.xhs, xq: this.queryParam.xq, xn: this.queryParam.xn})).then(res => {
            this.$refs.table.clearSelected()
            this.$message.success(res.msg)
            this.$refs.table.refresh(false)
          })
        },
        onCancel:() => {},
      })
    },
    openEdit (row) {
      this.set = true
      this.xh = row.xh
      this.$api.student.stuInfo(this.xh).then(res => {
        this.info = res.data
        this.info.xq = this.TYPE0072.find(xq => {
          return xq.code == this.systemInfo.currentSemester
        }).name
      })
    },
    send () {
      const form = this.form
      form.validateFields((err, values) => {
        if (err) {
          return
        }
        this.$api.student.registerCheckOne(Object.assign({xh: this.xh, xq: this.queryParam.xq, xn: this.queryParam.xn, note: values.note})).then(res => {
          form.resetFields()
          this.set = false
          this.$message.warning('操作成功')
        })
      })
    },
    yxChange (code) {
      this.$set(this.queryParam, 'sszym', '')
      if (code) {
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'zy', [])
      }
    }
  }
}
</script>
